var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgindex: [],
    pro_id:'',
    name:'',
    phone:'',
    address:'',
    cardName:'',
    productNum:'',
    productPrice:'',
    orderMoney:'',
    pic:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/myOrder/fxOrderDetailed?id=' + options.id + '&openid='+openid,
          success: function (res) {
            console.log(res.data)
            that.setData({
              pro_id: res.data.fxOrder.id,
              name: res.data.fxOrder.name,
              phone: res.data.fxOrder.phone,
              address: res.data.fxOrder.address,
              cardName: res.data.fxOrder.cardName,
              productNum: res.data.fxOrder.productNum,
              productPrice: res.data.fxOrder.procuctPrice,
              orderMoney: res.data.fxOrder.orderMoney,
              pic: res.data.fxOrder.picture
            })

            var imgList = that.data.pic.split('%')

            that.setData({
              imgList: imgList
            })
          }
        })
      },
    })
    
  },

  bindChooiceProduct: function () {
    let that = this;

    if (that.data.imgindex.length >= 3) {
      wx.showToast({
        title: '最多选择3张！',
      })
      return;
    }
    wx.chooseImage({
      count: 3,  //最多可以选择的图片总数
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        console.log('chooseImage',res)

        if (res.errMsg == 'chooseImage:ok') {       

          that.setData({
            imgindex: res.tempFilePaths
          })


          for (let i = 0; i < that.data.imgindex.length; i++){
            wx.getStorage({
              key: 'openid',
              success: function (res) {
                let openid = res.data

                wx.uploadFile({
                  url: app.globalData.URL + '/app/myOrder/imageUpload?openid=' + openid,
                  filePath: that.data.imgindex[i],
                  name: 'file',
                  header: {
                    "Content-Type": "multipart/form-data"
                  },
                  formData: {
                    "uid": that.data.pro_id,
                  },
                  success: function (res) {
                    console.log("myOrder/imageUpload:", res)
                    that.setData({
                      errMsg: res.errMsg
                    })

                    if (that.data.errMsg == "uploadFile:ok") {
                      wx.showToast({
                        title: '上传成功',
                        duration: 3000
                      })
                    } else {
                      wx.showToast({
                        title: '上传失败',
                        image: '../../images/error.png'
                      })
                    }
                  }
                })
              },
            })
          }

            
        }
      }
    })
  },
 
  //长按事件删除图片
  removeimge(e) {
    const that = this;
    let index = e.currentTarget.dataset.index;
    let imgindex = that.data.imgindex;
    imgindex.splice(index, 1);
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('用户点击确定')
          that.setData({
            imgindex: imgindex
          })
          wx.showToast({
            title: '删除成功',
            icon: 'none'
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  previewImg: function (e) {
    console.log('previewImg', e)
    var current = e.target.dataset.index;
    wx.previewImage({
      current: this.data.imgindex[current],
      urls: this.data.imgindex,
      success: function (res) {
        console.log('success');
      },
      fail: function (res) { },
    })
  }, 

  deleteImage: function (e) {
    var that = this;
    var images = that.data.imgs;
    console.log(images);
    var index = e.currentTarget.dataset.index; //获取当前长按图片下标
    console.log(index);
    wx.showModal({
      title: '系统提醒',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          images.splice(index, 1);
        } else if (res.cancel) {
          return false;
        }
        that.setData({
          files: images
        });
      }
    })
  },
  cancleOrder:function(){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.request({
          url: app.globalData.URL + '/app/myOrder/orderState?id=' + that.data.pro_id + '&orderState=0'+'&openid='+ openid ,
          success: function (res) {
            if (res.data.flag == 'true') {
              wx.showToast({
                title: '取消成功',
                success: function (res) {
                  wx.navigateBack({

                  })
                }
              })
            }
          }
        })
      },
    })
    
  },

  upload:function(){
    var that = this
    if (that.data.errMsg == "uploadFile:ok" || that.data.imgList.length > 0){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/myOrder/orderState?id=' + that.data.pro_id + '&orderState=3' + '&openid=' + openid ,
            success: function (res) {
              console.log(res)
              if (res.data.flag == true) {
                wx.showToast({
                  title: '上传成功',
                  success: function (res) {
                    wx.navigateBack({

                    })
                  }
                })
              }
            }
          })
        },
      })
    }else{
      wx.showToast({
        title: '请上传图片',
        image:'../../images/error.png'
      })
    } 
  }
})